// This is the main DLL file.

#include "stdafx.h"

#include "ManagedSail.h"

#include<vector>

using namespace System;
using namespace System::Reflection;
using namespace Runtime::InteropServices;

namespace ManagedSail
{
	Sail::Sail()
	{
		_sail = new sail();
	}

	void Sail::Release()
	{		
	}

	IntPtr Sail::GetBuffer()
	{
		IntPtr ptr = IntPtr(_sail->getBuffer());
		return ptr;
	}

	void Sail::SwapBuffer(SwapBufferMode swapMode)
	{		
		int res = _sail->swapBuffer((int)swapMode);
		if(res < 0)
			throw gcnew Exception("Cannot swap buffers: this node is not configured to stream pixels");
	}

	char* S2C(String^ s)
	{		
		using namespace Runtime::InteropServices;
		char* chars = (char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
		return chars;
	}

	void SetCharField(char* field, String^ s)
	{
		char* ca = S2C(s);
		strcpy(field, ca);
	}

	void Sail::Init(String ^fname,int resX,int resY)
	{
		::sailConfig config = ::sailConfig();
		char* chars = (char*)(Marshal::StringToHGlobalAnsi(fname)).ToPointer();

		if(config.init(chars) != 0)
			throw gcnew Exception("Couldn't load initialization data from " + fname);

		config.resX = resX;
		config.resY = resY;
		if(config.appName[0] == 0)
			strcpy(config.appName,"ManagedSail");
		int res = _sail->init(config);
		if(res < 0)
			throw gcnew Exception("Cannot initialize SAIL node");
	}
/*
	void Sail::Init(SailConfig^ conf)
	{
		::sailConfig config = ::sailConfig();		
		
		SetCharField(config.appName, conf->appName);
		SetCharField(config.masterIP, conf->masterIP);
		SetCharField(config.fsIP, conf->fsIP);
		SetCharField(config.bridgeIP, conf->bridgeIP);
		SetCharField(config.audioFileName, conf->audioFileName);
		SetCharField(config.streamIP, conf->streamIP);

		config.nwID			= conf->nwID			;
		config.fsPort			= conf->fsPort		;	
		config.bridgePort		= conf->bridgePort	;	
		config.bridgeOn		= conf->bridgeOn		;
		config.syncPort		= conf->syncPort		;
		config.msgPort			= conf->msgPort		;	
		config.nodeNum			= conf->nodeNum		;	
		config.appID			= conf->appID			;
		config.blockX			= conf->blockX		;	
		config.blockY			= conf->blockY		;	
		config.blockSize		= conf->blockSize		;
		config.winX			= conf->winX			;
		config.winY			= conf->winY			;
		config.winWidth		= conf->winWidth		;
		config.winHeight		= conf->winHeight		;
		config.protocol		= (nwProtocol)conf->protocol		;
		config.syncMode		= conf->syncMode		;
		config.streamMode		= conf->streamMode	;	
		config.rcvBufSize		= conf->rcvBufSize	;	
		config.rank			= conf->rank			;
		config.resX			= conf->resX			;
		config.resY			= conf->resY			;
		config.pixFmt			= (sagePixFmt)conf->pixFmt		;	
		config.rowOrd			= conf->rowOrd		;	
		config.master			= conf->master		;	
		config.rendering		= conf->rendering		;
		config.asyncUpdate		= conf->asyncUpdate	;	
		config.compression		= (sageCompressType)conf->compression	;	
		config.totalFrames		= conf->totalFrames	;	
		config.audioOn			= conf->audioOn		;	
		config.audioMode		= (sageAudioMode)conf->audioMode		;
		config.audioPort		= conf->audioPort		;
		config.audioDeviceNum	= conf->audioDeviceNum;	
		config.sampleFmt		= (sageSampleFmt)conf->sampleFmt		;
		config.samplingRate	= conf->samplingRate	;
		config.channels		= conf->channels		;
		config.framePerBuffer	= conf->framePerBuffer;	
		config.syncType		= (sageSyncType)conf->syncType		;
		config.audioBuffSize	= conf->audioBuffSize	;
		config.frameRate		= conf->frameRate		;
		config.bufWarnLimit	= conf->bufWarnLimit	;
		config.bufIncRatio		= conf->bufIncRatio		;
		config.maxBlockLoss	= conf->maxBlockLoss	;
		config.ackInterval		= conf->ackInterval	;	
		config.minSendRate		= conf->minSendRate;		
		config.maxSendRate		= conf->maxSendRate	;	
		config.senderCheckInterval = conf->senderCheckInterval;

		config.imageMap = sageRect(conf->imageMap->X, conf->imageMap->Y, conf->imageMap->Width, conf->imageMap->Height);

		//config = ::sailConfig();	


		int res = _sail->init(config);
		if(res < 0)
			throw gcnew Exception("Cannot initialize SAIL node");
	}
*/
	void Sail::Shutdown()
	{
		_sail->shutdown();
	}
}

